package Lab4;

import java.util.Date;
import java.util.Random;
import java.util.Timer;

/**
 * A class that represents a random array.
 *
 * @author Yibai Jiang
 * @version 1.5
 */
public class RandomArray {
    private int[] array; // instance variable

    /**
     * Constructor
     *
     * @param size The size of the array.
     */
    public RandomArray(int size) {
        Random random = new Random();
        array = new int[size];

        for (int a = 0; a < size; a++) {

            array[a] = random.nextInt(9);
        }
    }

    /**
     * A method to print the array elements.
     */
    public void printArray() {
        for (int a : array) {
            System.out.printf("%d ", a);

        }
        System.out.println();
    }

    /**
     * A method to calculate the sum of all elements.
     *
     * @return The sum.
     */
    public int calcSum() {
        int sum = 0;
        for (int a : array) {
            sum += a;
        }
        return sum;
    }

    /**
     * A method to calculate the mean (or average) of all elements.
     *
     * @return The mean.
     */
    public double calcMean() {
        int sum = calcSum();
        return (double) sum / array.length;
    }

    /**
     * A main method to test.
     */
    public static void main(String[] args) {
        // Check to see if the user has actually sent a parameter to the method.
        if (args.length != 1) {
            System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
            System.exit(-1);
        }
        // Create an instance of the class.
        RandomArray test = new RandomArray(Integer.parseInt(args[0]));

        // Print the array.
        test.printArray();

        // Calculate the sum of all the values in the array and print it.
        System.out.println("Sum: " + test.calcSum());
        // Calculate the mean of all the values in the array and print it.
        System.out.println("Mean: " + test.calcMean());
    }
}

